package com.dongdongshop.shiro;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.Exception.toTshowException;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.TbSellerService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.Objects;

//授权类
public class LoginUserRealm extends AuthorizingRealm {

    @Reference
    private TbSellerService tbSellerService;
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("进入授权方法");
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("进入登陆认证方法");
       UsernamePasswordToken token =  (UsernamePasswordToken)authenticationToken;
        TbSeller tbSeller = tbSellerService.selectByid(token.getUsername());
        if(!tbSeller.getSellerId().equals(token.getUsername())){
            return null;
        }
        if(Objects.equals(tbSeller.getStatus(),"0")){
            throw new toTshowException("该帐号正在审核中，请耐心等待");
        }
        if(Objects.equals(tbSeller.getStatus(),"2")){
            throw new toTshowException("该帐号审核失败，请联系客服");
        }
        if(Objects.equals(tbSeller.getStatus(),"3")){
            throw new toTshowException("该帐号已被关闭，请联系客服");
        }
        return new SimpleAuthenticationInfo(tbSeller,tbSeller.getPassword(), ByteSource.Util.bytes(tbSeller.getYan()),tbSeller.getSellerId());//需要最少三个参数
    }
}
